using AutomatedTrading.Data.Entity;
using Automation.Data.Helper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using MySqlHelper = Automation.Data.Helper.MySqlHelper;

namespace AutomatedTrading.Data.Service
{
    public class ProjectRemindRepository
    {
        private readonly MySqlHelper _mySqlHelper;

        public ProjectRemindRepository()
        {
            _mySqlHelper = new MySqlHelper();
        }

        public ProjectRemindRepository(MySqlHelper mySqlHelper)
        {
            _mySqlHelper = mySqlHelper;
        }

        public async Task<List<ProjectRemind>> GetAllAsync()
        {
            var sql = "SELECT * FROM project_remind";
            var dt = await Task.Run(() => _mySqlHelper.ExecuteDataTable(sql));
            return JsonHelper.DataTableToEntityArray<ProjectRemind>(dt);
        }

        public async Task<List<ProjectRemind>> GetByStatusAsync(int status)
        {
            var sql = "SELECT * FROM project_remind WHERE status = @Status";
            var param = new MySqlParameter("@Status", status);
            var dt = await Task.Run(() => _mySqlHelper.ExecuteDataTable(sql, param));
            var list = JsonHelper.DataTableToEntityArray<ProjectRemind>(dt);
            return list;
        }

        public async Task<ProjectRemind> GetHeartBeatByProjectAsync(string project)
        {
            var sql = "SELECT * FROM project_remind WHERE project = @Project and type=@Type";
            var parameters = new[]
           {
                new MySqlParameter("@Project", project ?? (object)DBNull.Value),
                new MySqlParameter("@Type", '0'),
            };
            var dt = await Task.Run(() => _mySqlHelper.ExecuteDataTable(sql, parameters));
            var list = JsonHelper.DataTableToEntityArray<ProjectRemind>(dt);
            return list.Count > 0 ? list[0] : null;
        }


        public async Task<int> InsertAsync(ProjectRemind entity)
        {
            var sql = @"INSERT INTO project_remind (project, intervalSecond, updateTime, needRemind,message,status,type)
                        VALUES (@project, @intervalSecond, @updateTime, @needRemind,@message,@status,@type)";
            var parameters = new[]
            {
                new MySqlParameter("@project", entity.project ?? (object)DBNull.Value),
                new MySqlParameter("@intervalSecond", entity.intervalSecond ?? (object)DBNull.Value),
                new MySqlParameter("@updateTime", entity.updateTime ?? (object)DBNull.Value),
                new MySqlParameter("@needRemind", entity.needRemind ?? (object)DBNull.Value),
                new MySqlParameter("@message", entity.message ?? (object)DBNull.Value),
                new MySqlParameter("@status", entity.status ?? (object)DBNull.Value),
                new MySqlParameter("@type", entity.type ?? (object)DBNull.Value),
            };
            return await Task.Run(() => _mySqlHelper.ExecuteInsertReturnId(sql, parameters));
        }

        public async Task<bool> UpdateAsync(ProjectRemind entity)
        {
            var sql = @"UPDATE project_remind SET
                        project = @project,
                        intervalSecond = @intervalSecond,
                        updateTime = @updateTime,
                        needRemind = @needRemind,
                        message=@message,
                        status=@status,
                        type=@type
                        WHERE ID = @ID";
            var parameters = new[]
            {
                new MySqlParameter("@ID", entity.ID),
                new MySqlParameter("@project", entity.project ?? (object)DBNull.Value),
                new MySqlParameter("@intervalSecond", entity.intervalSecond ?? (object)DBNull.Value),
                new MySqlParameter("@updateTime", entity.updateTime ?? (object)DBNull.Value),
                new MySqlParameter("@needRemind", entity.needRemind ?? (object)DBNull.Value),
                new MySqlParameter("@message", entity.message ?? (object)DBNull.Value),
                new MySqlParameter("@status", entity.status ?? (object)DBNull.Value),
                new MySqlParameter("@type", entity.type ?? (object)DBNull.Value),
            };
            int result = await Task.Run(() => _mySqlHelper.ExecuteNonQuery(sql, parameters));
            return result > 0;
        }

        public async Task<int> DeleteAsync(int id)
        {
            var sql = "DELETE FROM project_remind WHERE ID = @ID";
            var param = new MySqlParameter("@ID", id);
            return await Task.Run(() => _mySqlHelper.ExecuteNonQuery(sql, param));
        }
    }
}